#include "SpriteAnimation.h"

SpriteAnimation::SpriteAnimation(int frameHeight, int frameWidth, int loopTime, SDL_Surface* texture, bool loopable) {
	this->frameHeight = frameHeight;
	this->clip.h = frameHeight;
	this->frameWidth = frameWidth;
	this->clip.w = frameWidth;
	this->loopTime = loopTime;
	this->loopable = loopable;
	this->texture = texture;
	this->name = "Sprite_Animation";
	this->currFrame = 0;
	this->currAnimation = 0;
	this->elapsedTime = 0;

	if (texture != NULL) {
		int textWidth = texture->w;
		int textHeight = texture->h;
		this->numFrames = (textWidth / frameWidth);
	}
}

SpriteAnimation::~SpriteAnimation() {
	SDL_FreeSurface(texture);
}

void SpriteAnimation::setAnimation(int anim) {
	this->currAnimation = anim;
}

//Entity members
void SpriteAnimation::setXPos(double x) {
	this->xPos = x;
}

void SpriteAnimation::setYPos(double y) {
	this->yPos = y;
}

void SpriteAnimation::setName(std::string name) {
	this->name = name;
}

//Renderable members
void SpriteAnimation::setTexture(SDL_Surface* texture) {
	this->texture = texture;
}

void SpriteAnimation::setClip(SDL_Rect clip) {
	this->clip = clip;
}

void SpriteAnimation::update(Uint32 delta) {
	elapsedTime += delta;
	if (elapsedTime >= loopTime) {
		elapsedTime = 0;
		currFrame++;
		if (currFrame >= numFrames) {
			currFrame = 0;
		}
	}
	clip.x = (currFrame % (texture->w / clip.w)) * clip.w;
	if (currAnimation >= (texture->w / clip.w) || currAnimation < 0) {
		currAnimation = 0;
	}
	clip.y = currAnimation * 64;
}
